﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC_Class.Domain;
using RBAC_Class.ErrorCode;
using RBAC_Class.Read.API.Application.Command.Roles;
using RBAC_Class.Read.API.Dto;
using System;
using System.Threading.Tasks;
using NLog;

namespace RBAC_Class.Read.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<RoleController> logger;

        public RoleController(IMediator mediator, ILogger<RoleController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 角色查询
        /// </summary>
        /// <param name="cmd">命令</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<APIResult<APIPaging<List<RoleDto>>>> RoleQuery([FromQuery] RoleQueryCommand cmd)
        {
            try
            {
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "角色查询失败 - 参数: RoleName={RoleName}, PageIndex={PageIndex}, PageSize={PageSize}", 
                    cmd.RoleName, cmd.PageIndex, cmd.PageSize);
                throw;
            }
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        [HttpGet]
        public async Task<APIResult<List<Role>>> GetAllRole()
        {
            try
            {
                GetAllRoleCommand cmd = new GetAllRoleCommand();
                return await mediator.Send(cmd);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "获取所有角色失败");
                throw;
            }
        }
    }
}
